package projectpackage;

import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JSplitPane;
import javax.swing.JTextPane;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Dimension;
import java.awt.Color;
import java.awt.Font;
import java.awt.SystemColor;
import java.net.Socket;
import java.util.Observable;
import java.util.Observer;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JProgressBar;

public class Serverinterface extends JFrame implements Observer {

	private static final long serialVersionUID = -6896352174492310759L;
	private JPanel contentPane;
	private JTextPane Serverconsole;
	private JTextPane Variables;
	
	private JMenuBar menuBar;
	private JMenu mnFile;
	private JMenuItem mntmConnect;
	private JMenuItem mntmExit;
	
	private static Server s1;
	private static Thread thread;	
	private JSplitPane splitPane_1;
	private JProgressBar progressBar;
	private JTextPane Information;

	
	/**
	 * Create the frame.
	 */
	public Serverinterface() {
		setMinimumSize(new Dimension(600, 400));		
		setTitle("Server");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 600, 239);
		
		menuBar = new JMenuBar();
		setJMenuBar(menuBar);
		
		mnFile = new JMenu("File");
		menuBar.add(mnFile);
		
		mntmConnect = new JMenuItem("Connect");
		mntmConnect.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {					
				if(s1 == null)
				{
					s1 = new Server();
					s1.addObserver(s1.getframe());
					thread = new Thread(s1);
					thread.start();						
				}				
			}
		});
		mnFile.add(mntmConnect);
		
		mntmExit = new JMenuItem("Exit");
		mntmExit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.exit(-1);
			}
		});
		mnFile.add(mntmExit);
		contentPane = new JPanel();
		contentPane.setMinimumSize(new Dimension(600, 600));
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		
		JSplitPane splitPane = new JSplitPane();
		splitPane.setMinimumSize(new Dimension(179, 600));
		splitPane.setEnabled(false);
		contentPane.add(splitPane, BorderLayout.CENTER);
		
		Serverconsole = new JTextPane();
		Serverconsole.setMinimumSize(new Dimension(300, 300));
		Serverconsole.setEditable(false);
		Serverconsole.setFont(new Font("Tahoma", Font.BOLD, 12));
		Serverconsole.setBackground(Color.WHITE);
		Serverconsole.setForeground(SystemColor.activeCaptionText);
	
		splitPane.setRightComponent(Serverconsole);
		
		Variables = new JTextPane();
		Variables.setEditable(false);
		Variables.setFont(new Font("Tahoma", Font.BOLD, 12));
		Variables.setBackground(Color.WHITE);
		Variables.setMinimumSize(new Dimension(150, 300));
		splitPane.setLeftComponent(Variables);
		
		splitPane_1 = new JSplitPane();
		splitPane_1.setDividerSize(12);
		splitPane_1.setMaximumSize(new Dimension(400, 10));
		splitPane_1.setMinimumSize(new Dimension(179, 10));
		splitPane_1.setEnabled(false);
		contentPane.add(splitPane_1, BorderLayout.SOUTH);
		
		progressBar = new JProgressBar();
		progressBar.setEnabled(false);
		progressBar.setMinimumSize(new Dimension(10, 10));
		progressBar.setMaximumSize(new Dimension(32767, 12));
		progressBar.setMaximum(50);
		splitPane_1.setLeftComponent(progressBar);
		
		Information = new JTextPane();
		Information.setEditable(false);
		splitPane_1.setRightComponent(Information);
	}
	
	protected void handlestate(Serverstate state)
	{
		Serverconsole.setText(Serverconsole.getText() + state.consolestate);
		String tmp = "";		
		
		for(int i = 0; i < state.sockets.length; i++)
		{	
			if(state.sockets[i] != null)
			{
				tmp += state.sockets[i].getInetAddress().toString() + "\n";	
			}					
		}		
		Variables.setText(tmp);
		Information.setText("Clients connected : " + state.amountofclients);
		progressBar.setValue(state.procent);
	}
	public void append(String s, Socket[] sockets)
	{
		Serverconsole.setText(Serverconsole.getText() + s);
		String tmp = "";
		int countclients = 0;
		for(int i = 0; i < sockets.length; i++)
		{	
			if(sockets[i] != null)
			{
				tmp += sockets[i].getInetAddress().toString() + "\n";
				countclients++;
			}					
		}
		Information.setText("Clients connected : " + countclients);
		Variables.setText(tmp);
	}
	
	public void update(Observable arg0, Object arg1) {
		// TODO Auto-generated method stub		
		this.handlestate((Serverstate) arg1); 
	}

}
